//
//  ByDriverAuthenticationVC.m
//  hutouben_ios
//
//  Created by justiOS on 16/11/29.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByDriverAuthenticationVC.h"

@interface ByDriverAuthenticationVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *table;   // 主table
@property (nonatomic, strong) UITextField *nameTF;
@property (nonatomic, strong) UITextField *idcardTF;

// 上传身份证正面
@property (nonatomic, strong) UIImageView   *contentView1;
@property (nonatomic, strong) UIView        *emptyView1;
@property (nonatomic, assign) BOOL          isUpOne;
// 上传身份证背面
@property (nonatomic, strong) UIImageView   *contentView2;
@property (nonatomic, strong) UIView        *emptyView2;
@property (nonatomic, assign) BOOL          isUpTwo;

@property (nonatomic, strong) UIImageView   *contentView3;
@property (nonatomic, strong) UIView        *emptyView3;
@property (nonatomic, assign) BOOL          isUpThree;

@property (nonatomic, strong) ByIconChooseDataSource *iconChooseDataSource1;
@property (nonatomic, strong) ByIconChooseDataSource *iconChooseDataSource2;
@property (nonatomic, strong) ByIconChooseDataSource *iconChooseDataSource3;
@end

@implementation ByDriverAuthenticationVC
#pragma mark - getter
- (UIImageView *)contentView1 {
    if (!_contentView1) {
        _contentView1 = [[UIImageView alloc] init];
        _contentView1.frame = self.emptyView1.bounds;
        _contentView1.hidden = YES;
        [self.emptyView1 addSubview:_contentView1];
    }
    return _contentView1;
}
- (ByIconChooseDataSource *)iconChooseDataSource1 {
    if (!_iconChooseDataSource1) {
        _iconChooseDataSource1 = [[ByIconChooseDataSource alloc] init];
        [_iconChooseDataSource1 fireWithViewController:self];
    }
    return _iconChooseDataSource1;
}
- (UIImageView *)contentView2 {
    if (!_contentView2) {
        _contentView2 = [[UIImageView alloc] init];
        _contentView2.frame = self.emptyView2.bounds;
        _contentView2.hidden = YES;
        [self.emptyView2 addSubview:_contentView2];
    }
    return _contentView2;
}
- (ByIconChooseDataSource *)iconChooseDataSource2 {
    if (!_iconChooseDataSource2) {
        _iconChooseDataSource2 = [[ByIconChooseDataSource alloc] init];
        [_iconChooseDataSource2 fireWithViewController:self];
    }
    return _iconChooseDataSource2;
}

- (UIImageView *)contentView3 {
    if (!_contentView3) {
        _contentView3 = [[UIImageView alloc] init];
        _contentView3.frame = self.emptyView3.bounds;
        _contentView3.hidden = YES;
        [self.emptyView3 addSubview:_contentView3];
    }
    return _contentView3;
}
- (ByIconChooseDataSource *)iconChooseDataSource3 {
    if (!_iconChooseDataSource3) {
        _iconChooseDataSource3 = [[ByIconChooseDataSource alloc] init];
        [_iconChooseDataSource3 fireWithViewController:self];
    }
    return _iconChooseDataSource3;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"司机认证";
    [self addLeftBackBarButtonItem];
    [self createTableView];
    [self createButton];
}

- (void)createButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, SCREEN_HEIGHT - 64 - 49, SCREEN_WIDTH, 49);
    button.backgroundColor = HTB_THEME_COLOR;
    button.titleLabel.font = FONT(16);
    [button setTitle:@"提交认证" forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(uploadInfoActon:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)createTableView {
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT -64 - 49) style:UITableViewStylePlain];
    self.table.backgroundColor = HTB_BG_GRAY;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:_table];
    
}


#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    

    if (indexPath.row == 0) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,20, SCREEN_WIDTH -20, 40)];
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = HTB_TEXT_COLOR1;
        label.text = @"个人信息";
        label.font = FONT(18);
        [cell addSubview:label];
        cell.backgroundColor = [UIColor clearColor];
        
        UIView *sep1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
        sep1.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
        [cell addSubview:sep1];
        sep1.bottom = 60;
        
        self.nameTF = [[UITextField alloc] initWithFrame:CGRectMake(0, sep1.bottom, SCREEN_WIDTH, 50)];
        _nameTF.backgroundColor = [UIColor whiteColor];
        _nameTF.placeholder = @"请输入真实姓名";
        UIImageView *imgview1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        imgview1.image = [UIImage imageNamed:@"mine_name"];
        _nameTF.leftViewMode = UITextFieldViewModeAlways;
        _nameTF.leftView =  imgview1;
        [cell addSubview:_nameTF];
        UIView *sep2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
        sep2.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
        [cell addSubview:sep2];
        sep2.bottom = 110;
        
        self.idcardTF = [[UITextField alloc] initWithFrame:CGRectMake(0, sep2.bottom, SCREEN_WIDTH, 50)];
        _idcardTF.backgroundColor = [UIColor whiteColor];
        _idcardTF.placeholder = @"请输入身份证号";
        UIImageView *imgview2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        imgview2.image = [UIImage imageNamed:@"mine_identity"];
        _idcardTF.leftViewMode = UITextFieldViewModeAlways;
        _idcardTF.leftView =  imgview2;
        [cell addSubview:_idcardTF];
        UIView *sep3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
        sep3.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
        [cell addSubview:sep3];
        sep3.bottom = 160;
    }else{
        cell.backgroundColor = [UIColor clearColor];
        UILabel *idlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 40)];
        idlabel.textAlignment = NSTextAlignmentLeft;
        idlabel.textColor = HTB_TEXT_COLOR1;
        idlabel.text = @"上传身份证正反面";
        idlabel.font = FONT(18);
        [cell addSubview:idlabel];
        
        NSArray *sels = @[@"tapAction1", @"tapAction2"];
        for (int i = 0; i < 2; i ++) {

            /** 凭证上传视图 */
            UIView *emptyView = [[UIView alloc] init];
            if (i == 0) {
                emptyView.frame = CGRectMake(10, idlabel.bottom + 10, (SCREEN_WIDTH - 30)/2.0, (SCREEN_WIDTH - 30)/3.0);
            }else{
                emptyView.frame = CGRectMake(SCREEN_WIDTH / 2 + 5, idlabel.bottom + 10, (SCREEN_WIDTH - 30)/2.0, (SCREEN_WIDTH - 30)/3.0);
            }
            [cell addSubview:emptyView];
            emptyView.layer.cornerRadius = 3;
            emptyView.layer.borderColor = rgba(177, 220, 251, 1).CGColor;
            emptyView.layer.borderWidth = 1;
            emptyView.clipsToBounds = YES;
            emptyView.backgroundColor = [UIColor whiteColor];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:NSSelectorFromString(sels[i])];
            [emptyView addGestureRecognizer:tap];      // 添加点击手势
            
 
            UILabel *line1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, emptyView.width / 2, emptyView.height / 2)];
            line1.numberOfLines = 0;
            line1.textColor = rgba(177, 220, 251, 1);
            line1.font = FONT(14);
            line1.textAlignment = NSTextAlignmentCenter;
            if (i == 0) {
                line1.text = @"点击上传\n（正面）";
            }else{
                line1.text = @"点击上传\n（反面）";
            }
            [emptyView addSubview:line1];
            
            line1.center = CGPointMake(emptyView.width / 2, emptyView.height / 2);
            
            if (i == 0) {
                self.emptyView1 = emptyView;
            } else {
                self.emptyView2 = emptyView;
            }
            
        }
        
        UILabel *dvlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, idlabel.bottom + 20 +(SCREEN_WIDTH - 30)/3.0, SCREEN_WIDTH - 20, 40)];
        dvlabel.textAlignment = NSTextAlignmentLeft;
        dvlabel.textColor = HTB_TEXT_COLOR1;
        dvlabel.text = @"上传行驶证";
        dvlabel.font = FONT(18);
        [cell addSubview:dvlabel];

        
        UIView *emptyView = [[UIView alloc] init];
        emptyView.frame = CGRectMake(10, dvlabel.bottom + 10, (SCREEN_WIDTH - 30)/2.0, (SCREEN_WIDTH - 30)/3.0);
        [cell addSubview:emptyView];
        emptyView.layer.cornerRadius = 3;
        emptyView.layer.borderColor = rgba(177, 220, 251, 1).CGColor;
        emptyView.layer.borderWidth = 1;
        emptyView.backgroundColor = [UIColor whiteColor];
        emptyView.clipsToBounds = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction3)];
        [emptyView addGestureRecognizer:tap];      // 添加点击手势
        UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, emptyView.width, emptyView.height )];
        textlabel.numberOfLines = 0;
        textlabel.textColor = rgba(177, 220, 251, 1);
        textlabel.font = FONT(14);
        textlabel.textAlignment = NSTextAlignmentCenter;
        textlabel.text = @"点击上传\n（行驶证主页）";
        [emptyView addSubview:textlabel];
        textlabel.center = CGPointMake(emptyView.width / 2, emptyView.height / 2);
        self.emptyView3 = emptyView;
        
    }
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 160;
    }
    return 375;
}


// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
}



#pragma mark 点击事件
// 提交认证
- (void)uploadInfoActon:(UIButton *)button {
    if (_isUpOne == NO) {
        [ByCoreToolAlert showOnlyText:@"请上传身份证正面照"];
        return;
    }
    if (_isUpTwo == NO) {
        [ByCoreToolAlert showOnlyText:@"请上传身份证背面照"];
        return;
    }
    if (_isUpThree == NO) {
        [ByCoreToolAlert showOnlyText:@"请上传行驶证"];
        return;
    }
    
    if (_nameTF.text.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请填写姓名"];
        return;
    }
    
    if (_idcardTF.text.length != 18) {
        [ByCoreToolAlert showOnlyText:@"请填写正确身份证号"];
        return;
    }
    
    NSLog(@"提交认证");

}


- (void)tapAction1 {
    [self.iconChooseDataSource1 makeConfigToSelectIcon];
    __weak typeof(self) weakself = self;
    [self.iconChooseDataSource1 setCompleteBlock:^(UIImage *image) {
        
        CGFloat ratio = self.emptyView1.width / self.emptyView1.height;
        image = [image getCenterRatilImageWithRatio:ratio];
        weakself.contentView1.image = image;
        weakself.contentView1.hidden = NO;
        _isUpOne = YES;
    }];
}
- (void)tapAction2 {
    [self.iconChooseDataSource2 makeConfigToSelectIcon];
    __weak typeof(self) weakself = self;
    [self.iconChooseDataSource2 setCompleteBlock:^(UIImage *image) {
        
        CGFloat ratio = self.emptyView2.width / self.emptyView2.height;
        image = [image getCenterRatilImageWithRatio:ratio];
        weakself.contentView2.image = image;
        weakself.contentView2.hidden = NO;
        _isUpTwo = YES;
    }];
}

- (void)tapAction3 {
    [self.iconChooseDataSource3 makeConfigToSelectIcon];
    __weak typeof(self) weakself = self;
    [self.iconChooseDataSource3 setCompleteBlock:^(UIImage *image) {
        
        CGFloat ratio = self.emptyView3.width / self.emptyView3.height;
        image = [image getCenterRatilImageWithRatio:ratio];
        weakself.contentView3.image = image;
        weakself.contentView3.hidden = NO;
        _isUpThree = YES;
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
